package com.sunday.aomicaichang.ui.my;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.adpter.WalletDetailAdapter;
import com.sunday.aomicaichang.http.ApiClient;
import com.sunday.aomicaichang.model.Wallet;
import com.sunday.member.utils.SignUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/6.
 */

public class WalletDetailActivity extends BaseActivity {
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private WalletDetailAdapter adapter;
    private List<Wallet> dataSet=new ArrayList<>();
    private boolean isCanloadMore;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet_detail);
        ButterKnife.bind(this);

        initRecyclerView();
    }
    private LinearLayoutManager layoutManager;
    private void initRecyclerView(){
        layoutManager=new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).drawable(R.drawable.horizontal_space_divider).build());
        adapter=new WalletDetailAdapter(mContext);
        adapter.setmData(dataSet);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        getData();
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            private int lastVisibleItem=1;
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && lastVisibleItem + 1 == adapter.getItemCount() && isCanloadMore) {
                    getData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });

    }


    private int pageNo=1;
    private int pageSize=10;
    private void getData() {

        String nonce= MD5Utils.random();
        String[] str1=new String[]{"pageSize","pageNumber","nonce"};
        String[] str2=new String[]{String.valueOf(pageSize),String.valueOf(pageNo),nonce};
        showLoadingDialog(0);
        Call<ResultDO<List<Wallet>>> call= ApiClient.getApiAdapter().record(pageSize,pageNo,nonce,
                SignUtil.getSign(str1,str2));
        call.enqueue(new Callback<ResultDO<List<Wallet>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Wallet>>> call, Response<ResultDO<List<Wallet>>> response) {
                dissMissDialog();
                if (isFinish||response.body()==null){return;}
                if (response.body().getCode()==0){
                    if (response.body().getResult()==null){return;}
                    if (pageNo==1){
                        if(dataSet!=null)
                        dataSet.clear();
                    }
                    dataSet.addAll(response.body().getResult());
                    if (dataSet.size()==10){
                        isCanloadMore=true;
                        pageNo++;
                    }else {
                        isCanloadMore=false;
                    }

                    adapter.setmData(dataSet);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Wallet>>> call, Throwable t) {
            }
        });

    }
}
